package com.wingsoft.basic.service.impl;

import com.wingsoft.common.exception.ServiceException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ObjectUtils;
import com.wingsoft.common.utils.bean.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wingsoft.basic.mapper.CaseInfoMapper;
import com.wingsoft.basic.domain.CaseInfo;
import com.wingsoft.basic.service.ICaseInfoService;
import com.wingsoft.common.utils.IdGenerator.IdGeneratorFactory;
import com.wingsoft.basic.dto.CaseInfoDto;
import com.wingsoft.basic.vo.CaseInfoVo;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 案例Service业务层处理
 *
 * @author hemj
 * @date 2025-10-25
 */
@Service
public class CaseInfoServiceImpl extends MPJBaseServiceImpl<CaseInfoMapper, CaseInfo> implements ICaseInfoService
{
    @Autowired
    private IdGeneratorFactory idGeneratorFactory;

    /**
         * 验证参数
         */
    private void validateData(CaseInfo caseInfo){
        if (ObjectUtils.isEmpty(caseInfo.getTaskType())){
            throw new ServiceException("任务类型，1为文生视频，2为图+文生视频不能为空");
        }
        if (ObjectUtils.isEmpty(caseInfo.getDes())){
            throw new ServiceException("视频描述不能为空");
        }
        if (ObjectUtils.isEmpty(caseInfo.getVideoType())){
            throw new ServiceException("画面模式，1为横版，2为竖版，3为高清横版，4为高清竖版不能为空");
        }
        if (ObjectUtils.isEmpty(caseInfo.getSeconds())){
            throw new ServiceException("视频时长不能为空");
        }
    }

    /**
     * 保存数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public CaseInfo saveCascade(CaseInfoDto caseInfoDto){
        CaseInfo caseInfo = new CaseInfo();
        BeanUtils.copyProperties(caseInfoDto,caseInfo);
        validateData(caseInfo);
        caseInfo.setId(idGeneratorFactory.getId(CaseInfo.class.getSimpleName()));
        this.save(caseInfo);
        return caseInfo;
    }
    /**
     * 编辑数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public CaseInfo updateCascade(CaseInfoDto caseInfoDto){
        CaseInfo caseInfo = this.getById(caseInfoDto.getId());;
        BeanUtils.copyPropertiesIgnoreNull(caseInfoDto,caseInfo);
        validateData(caseInfo);
        this.updateById(caseInfo);
        return caseInfo;
    }

    /**
     * 初始化查询条件
     */
    public MPJLambdaWrapper<CaseInfo> initQueryWrapper(CaseInfoDto caseInfoDto){
        MPJLambdaWrapper<CaseInfo> queryWrapper = new MPJLambdaWrapper<>();
        if (!ObjectUtils.isEmpty(caseInfoDto.getTaskType())) {
            queryWrapper.eq(CaseInfo::getTaskType, caseInfoDto.getTaskType());
        }
        if (!ObjectUtils.isEmpty(caseInfoDto.getPicUrl())) {
            queryWrapper.eq(CaseInfo::getPicUrl, caseInfoDto.getPicUrl());
        }
        if (!ObjectUtils.isEmpty(caseInfoDto.getDes())) {
            queryWrapper.eq(CaseInfo::getDes, caseInfoDto.getDes());
        }
        if (!ObjectUtils.isEmpty(caseInfoDto.getVideoType())) {
            queryWrapper.eq(CaseInfo::getVideoType, caseInfoDto.getVideoType());
        }
        if (!ObjectUtils.isEmpty(caseInfoDto.getVideoUrl())) {
            queryWrapper.eq(CaseInfo::getVideoUrl, caseInfoDto.getVideoUrl());
        }
        if (!ObjectUtils.isEmpty(caseInfoDto.getSeconds())) {
            queryWrapper.eq(CaseInfo::getSeconds, caseInfoDto.getSeconds());
        }
        queryWrapper.orderByDesc(CaseInfo::getId);
        return queryWrapper;
    }

    /**
     * list查询
     */
    public List<CaseInfoVo> queryList(CaseInfoDto caseInfoDto) {
        List<CaseInfo> caseInfos = this.selectJoinList(CaseInfo.class,initQueryWrapper(caseInfoDto));
        List<CaseInfoVo> vos = new ArrayList<>();
        for (CaseInfo caseInfo:caseInfos) {
            CaseInfoVo caseInfoVo = new CaseInfoVo();
            BeanUtils.copyProperties(caseInfo,caseInfoVo);
            vos.add(caseInfoVo);
        }
        return vos;
    }

    /**
     * 统计
     */
    public long countList(CaseInfoDto caseInfoDto) {
        return this.count(initQueryWrapper(caseInfoDto));
    }

    /**
     * 获取单个数据
     */
    public CaseInfoVo getOneById(Long id){
        CaseInfo caseInfo = this.getById(id);
        CaseInfoVo caseInfoVo = new CaseInfoVo();
        BeanUtils.copyProperties(caseInfo,caseInfoVo);
        return caseInfoVo;
    }

    /**
     * 删除数据
     * @param ids
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public void removeByIdList(List<Long> ids) {
        this.removeByIds(ids);
    }

}
